﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Windows.Forms;

namespace ScreenTools {
    public partial class ScreenCaptureControl : UserControl {
        private Bitmap _capture;

        public ScreenCaptureControl() {
            InitializeComponent();
        }

        private void _captureWholeScreen_Click(object sender, EventArgs e) {
            this.ParentForm.Hide();
            Thread.Sleep(100);

            try {
                int minX = int.MaxValue;
                int maxX = int.MinValue;
                int minY = int.MaxValue;
                int maxY = int.MinValue;
                foreach (Screen screen in Screen.AllScreens) {
                    Rectangle bounds = screen.Bounds;
                    if (bounds.Left < minX) minX = bounds.Left;
                    if (bounds.Right > maxX) maxX = bounds.Right;
                    if (bounds.Top < minY) minY = bounds.Top;
                    if (bounds.Bottom > maxY) maxY = bounds.Bottom;
                }
                int width = maxX - minX;
                int height = maxY - minY;

                if (_capture != null) {
                    _capture.Dispose();
                    _capture = null;
                }
                _capture = new Bitmap(width, height, PixelFormat.Format32bppArgb);
                Graphics g = Graphics.FromImage(_capture);
                g.CopyFromScreen(minX, minY, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);

                _previewWnd.Refresh();
                CopyToClipboard();
            } finally {
                this.ParentForm.Show();
            }
        }

        private void _previewWnd_Paint(object sender, PaintEventArgs e) {
            e.Graphics.Clear(SystemColors.Control);
            if (_capture != null) {
                using (Pen penBorder = new Pen(Color.White)) {
                    Rectangle sourceRect = new Rectangle(0, 0, _capture.Width, _capture.Height);
                    Rectangle destRect = new Rectangle(1, 1, _previewWnd.Width - 2, _previewWnd.Height - 2);

                    int newWidth1 = (int)(((double)destRect.Height / _capture.Height) * _capture.Width);
                    int newHeight1 = destRect.Height;

                    int newWidth2 = destRect.Width;
                    int newHeight2 = (int)(((double)destRect.Width / _capture.Width) * _capture.Height);

                    int newWidth, newHeight;
                    if (newWidth1 <= destRect.Width) {
                        newWidth = newWidth1;
                        newHeight = newHeight1;
                    } else {
                        newWidth = newWidth2;
                        newHeight = newHeight2;
                    }

                    destRect = new Rectangle(1, 1, newWidth, newHeight);
                    e.Graphics.DrawImage(_capture, destRect, sourceRect, GraphicsUnit.Pixel);
                    destRect.Inflate(1, 1);
                    e.Graphics.DrawRectangle(penBorder, destRect);
                }
            }
        }

        private void _copyToClipboard_Click(object sender, EventArgs e) {
            CopyToClipboard();
        }

        private void CopyToClipboard() {
            Clipboard.SetImage(_capture);
        }

        private void _preview_Resize(object sender, EventArgs e) {
            _previewWnd.Refresh();
        }
    }
}
